<?php

// Set for Money format
setlocale(LC_MONETARY, 'en_US');

// Include Bill class
require_once dirname(__FILE__) . '/../models/bill.php';
// Include When class
require_once('When.php');


/**
 * Utils Libary, used by Biller Model. These functions are broken out into this 
 * library so they are unit testable.
 *
 */
class Utils {

    /**
     * Sort the bills with a user defined sort
     * List passed by reference to save memory
     */
    function sort_bills(&$list) {
        usort($list, array('Utils', 'compare_bill_due_date'));
    }

    /**
     * Used by the user defined sort to sort Bills by date 
     */
    function compare_bill_due_date($a, $b) {
        if ($a->due_date == $b->due_date) {
            return 0;
        }
        return ($a->due_date < $b->due_date) ? -1 : 1;
    }

    /**
     * Pass in Bill info and RRULE get back a list of bills 
     * Pass return array by reference to save memory
     */
    function expand_bills(&$list, $start, $end, $id, $name, $amount, $rrule) {

        // Create new When object
        $when = new When();

        // Find the recurrence of the rrule
        $when->recur($start)->until($end)->rrule($rrule);

        // Loop thru the results adding to the list
        while ($result = $when->next()) {
            // Add the bill
            $list[] = new Bill($id, $name, number_format($amount, 2), $result);
        }
    }

}

?>
